package com.doit.day03

import scala.io.Source

/*
join
left join

搞一个map ==》 key value

order.txt
order011,u001,300
order012,u002,200
order023,u006,100
order056,u007,300
order066,u003,500
order055,u004,300
order021,u005,300
order014,u001,100
order025,u005,300
order046,u007,30
order067,u003,340
order098,u008,310

user.txt
u001,hls,22,fengjie
u002,wangwu,31,lisi
u003,zhangyanru,22,tananpengyou
u004,laocao,26,fengyi
u005,mengqi,12,nvmengqi
u006,haolei,38,sb
u007,wanghongjing,24,wife
u009,wanghongjing,24,wife
 */
object _09_模拟join操作 {
  def main(args: Array[String]): Unit = {
    val order: List[String] = Source.fromFile("data/input/order.txt").getLines().toList
    val user: List[String] = Source.fromFile("data/input/user.txt").getLines().toList

    val orders: List[(String, String, String)] = order.map(line => {
      //order098,u008,310
      val arr: Array[String] = line.split(",")
      (arr(0), arr(1), arr(2))
//      (arr(1),(arr(0),arr(2)))   不能转变成map，如果转变成map可能会数据丢失
    })

    val map: Map[String, (String, String, String)] = user.map(line => {
      //u009,wanghongjing,24,wife
      val arr: Array[String] = line.split(",")
      (arr(0), (arr(1), arr(2), arr(3)))
    }).toMap

//    orders.map(tp=>{
//      val value: String = tp._2
//      val tuple: (String, String, String) = map.getOrElse(value, (null, null, null))
//      (tp._1,tp._3,tuple)
//    }).foreach(println)

    for (elem <- orders) {
      val maybeTuple: Option[(String, String, String)] = map.get(elem._2)
      if (!maybeTuple.isEmpty ){
        val value: (String, String, String) = maybeTuple.get
        println((elem._1,elem._3,value))
      }
    }



  }
}
